package com.seata.cloud.response;

/**
 * 响应信息主体
 *
 * @param <T>
 * @author lengleng
 */
public class Result<T> {


    public static final int SUCCESS = 0;

    public static final int FAIL = 1;
    /**
     * token失效或者token为空时的情况
     */
    public static final int NEED_LOGIN = 2;
    /**
     * 被挤下线的状态
     */
    public static final int LOGIN_AGAIN = 3;
    
    /**
     * 内容不存在的状态
     */
    public static final int NULL_CONTENT = 4;

    private String msg = "success";

    private int code = SUCCESS;

    private T data;
    
    private T attach;

    public Result() {
        super();
    }
    
    public Result(T data) {
        super();
        this.data = data;
    }
    
    public Result(T data, String msg) {
        super();
        this.data = data;
        this.msg = msg;
    }

    public Result(Throwable e) {
        super();
        this.msg = e.getMessage();
        this.code = FAIL;
    }
    public Result(int code){
        super();
        this.code = code;
    }

    public static Result error() {
        Result result = new Result();
        result.setCode(FAIL);
        result.setMsg("响应失败!");
        return result;
    }

    public static <E> Result error(E data) {
        Result result = new Result();
        result.setCode(FAIL);
        result.setData(data);
        result.setMsg("响应失败!");
        return result;
    }
    
    public boolean isSuccess() {
    	return this.code == 0;
    }
    
    public static Result errorMsg(String msg) {
        Result result = new Result();
        result.setCode(FAIL);
        result.setMsg(msg);
        return result;
    }

    public static Result successMsg(String msg) {
        Result result = new Result();
        result.setCode(SUCCESS);
        result.setMsg(msg);
        return result;
    }

    public static Result success() {
        Result result = new Result();
        result.setCode(SUCCESS);
        result.setMsg("响应成功!");
        return result;
    }

    public static <E> Result success(E data) {
        Result result = new Result();
        result.setCode(SUCCESS);
        result.setData(data);
        result.setMsg("响应成功!");
        return result;
    }

    public static <E> Result success(E data,E attach) {
        Result result = new Result();
        result.setCode(SUCCESS);
        result.setData(data);
        result.setAttach(attach);
        result.setMsg("响应成功!");
        return result;
    }

    public static <E> Result success(String msg,E data){
        Result result = new Result();
        result.setCode(SUCCESS);
        result.setData(data);
        result.setMsg(msg);
        return result;
    }
    
    public static <E> Result error(String msg,E data){
        Result result = new Result();
        result.setCode(FAIL);
        result.setData(data);
        result.setMsg(msg);
        return result;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public T getAttach() {
        return attach;
    }

    public void setAttach(T attach) {
        this.attach = attach;
    }
}
